try {
const [usersRes, roleMapRes] = await Promise.all([
usersApi.getUsers(),
roleMappingApi.getRoleMappings(),
]);
this.setState((prevState) => ({
...generateStateData(usersRes.data, prevState.locations, prevState.roles, roleMapRes.data),
}));
resolve();
} catch (err) {
reject();
return;
}
resolve();
})
handlePasswordTextChange = (evt) => this.setState({ passwordText: evt.target.value })
handlePasswordSubmit = (event) => {
event.preventDefault();
if (!this.state.passwordText) {
this.setState({
passwordDialogError: true,
passwordDialogErrorMessage: 'Please enter a password.',
});
return;
}
this.setState({ passwordDialogOpen: false, passwordDialogError: false });
if (this.passwordDialogResolve) {
this.passwordDialogResolve(this.state.passwordText);
this.passwordDialogResolve = null;
}
}
render() {
if (this.props.error) {
return (<ErrorPage message={this.props.errorMessage} />);
}
const { classes } = this.props;
const { userData, locationLookup, roleLookup } = this.state;
return (
<div className={classes.root}>
<Dialog
open={this.state.passwordDialogOpen}
onClose={() => {
if (this.passwordDialogResolve) {
this.passwordDialogResolve(null);
this.passwordDialogResolve = null;
}
this.setState({ passwordDialogOpen: false });
}}
>
<div className={this.props.classes.dialogContainer}>
<Typography>
{this.state.passwordDialogQuestion}
</Typography>
<form className={classes.form} onSubmit={this.handlePasswordSubmit}>
{this.state.passwordDialogError &&
<FormHelperText error className={classes.errorText}>
{this.state.passwordDialogErrorMessage}
</FormHelperText>
}
<FormControl
margin="normal"
required
fullWidth
error={this.state.error}
>
<InputLabel htmlFor="password">New Password</InputLabel>
<Input
id="password"
name="password"
autoComplete="password"
autoFocus
value={this.state.passwordText}
onChange={this.handlePasswordTextChange}
/>
</FormControl>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Save
</Button>
</form>
</div>
</Dialog>
<Notifications ref={this.notificationsRef} />
<div className={this.props.classes.table}>
<MaterialTable
columns={[
{ title: 'First Name', field: 'firstName' },
{ title: 'Last Name', field: 'lastName' },
{ title: 'Email', field: 'email' },
{
title: 'Role',
field: 'role',
lookup: roleLookup,
},
{
title: 'Department',
field: 'locationName',
lookup: locationLookup,
},
]}
actions={[
{
icon: 'settings_backup_restore',
tooltip: 'Reset User Password',
onClick: async (event, rowData) => {
const usersApi = new UsersAPI(this.props.token);
const password = await this.getPassword('Please enter the users new temporary password:');
if (password) {
try {
await usersApi.resetPasswordByAdmin(rowData.id, password);
this.notificationsRef.current.showNotification('success', 'Password updated.');
} catch (err) {
this.notificationsRef.current.showNotification('error', 'An error occured while updating the password.');
}
}
},
iconProps: {
style: {
fontSize: '24px',
},
},
},
]}
data={userData}
title="User Management"
options={{
pageSize: 25,
pageSizeOptions: [25, 50, 100],
exportButton: true,
emptyRowsWhenPaging: false,
addRowPosition: 'first',
}}
editable={{
onRowAdd: this.onRowAdd,
onRowUpdate: this.onRowUpdate,
onRowDelete: this.onRowDelete,
}}
/>
</div>
</div>
);
}
}
export default User;